package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.Depts;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.respBean.DeptTree;
import com.chtw.oasystem.service.impl.DeptServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-18 23:14
 */
@ResponseBody
@RestController
@RequestMapping("/dept")
public class DeptController {
    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptServiceImpl deptService;

    @GetMapping("/list")
    public RespUtils<List<Depts>> list(){
        try {
            return RespUtils.success(deptService.list());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/create")
    public RespUtils<Boolean> create(HttpSession session, @RequestBody Depts depts){
        try{
            boolean isCreated = deptService.create(session, depts);
            if (isCreated) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("添加部门失败！");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/edit")
    public RespUtils<Boolean> edit(HttpSession session, @RequestBody Depts depts){
        try{
            boolean isEdit = deptService.edit(session, depts);
            if (isEdit) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("编辑部门失败！");
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return RespUtils.fail(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    public RespUtils<Boolean> detele(HttpSession session, String dept_id){
        try{
            boolean isDel = deptService.deleteDept(session, dept_id);
            if (isDel) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("删除部门失败！");
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/info")
    public RespUtils<Depts> info(String dept_id){
        try{
            return RespUtils.success(deptService.info(dept_id));
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/tree")
    public RespUtils<List<DeptTree>> tree(){
        try{
            return RespUtils.success(deptService.tree());
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return RespUtils.fail(e.getMessage());
        }
    }
}
